package com.yonyou.crm.bpub.initData.service;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.bpub.initData.entity.*;
import com.yonyou.crm.common.exception.CrmBusinessException;
import com.yonyou.crm.cum.customer.entity.CustomerSalesVO;
import com.yonyou.crm.cum.customer.entity.CustomerVO;
import com.yonyou.crm.cum.customer.service.CustomerBizServiceImpl;

@Service
public class CustomerInitDatas extends BaseInitDatas{
	
	@Autowired
	CustomerBizServiceImpl service;
	
	private String[] itemkeyOfEnumList = {"level", "type", "state","industry", "cannelType", "source"};
	
	private String[] itemkeyOfPicList = {"taxCertificate", "bizLicense", "orgCertificate", "docFront", "docReverse"};
	
	@Override
	public <T extends Serializable> T getDetail(long id) {
		return (T)service.getDetail(id);
	}

	@Override
	public ArrayList<InitDatasListItemVO> getHeader(InitDatasVO initData) {
		ArrayList<InitDatasListItemVO> headerList = initData.getHeaderlist();
		if ( headerList != null && headerList.size() > 0) {
			for(InitDatasListItemVO headerItem : headerList) {
				String itemkey = headerItem.getItemkey();
				if ("province_city_district".equals(itemkey)) {
					//给省市区添加扩展结构(经纬度)
					List<InitDatasItemVO> itemDataList = headerItem.getItemdatalist();
					InitDatasItemVO itemdata = itemDataList.get(0);
					List<InitParamItemVO> paramList = new ArrayList<>();
					InitParamItemVO paramItem;
					paramItem = new InitParamItemVO("longitude","");
					paramList.add(paramItem);
					paramItem = new InitParamItemVO("latitude","");
					paramList.add(paramItem);
					itemdata.setParamlist(paramList);
				} else if ("name".equals(itemkey)) {
					//给客户名称添加扩展结构(客户认证)
					List<InitDatasItemVO> itemDataList = headerItem.getItemdatalist();
					InitDatasItemVO itemdata = itemDataList.get(0);
					List<InitParamItemVO> paramList = new ArrayList<>();
					InitParamItemVO paramItem;
					paramItem = new InitParamItemVO("isIdentified","N");
					paramList.add(paramItem);
					itemdata.setParamlist(paramList);
				}
			}
		} else {
			headerList = new ArrayList<InitDatasListItemVO>();
		}
		return headerList;
	}

	@Override
	public ArrayList<InitDatasBodyVO> getBody(InitDatasVO layoutList) {
		ArrayList<InitDatasBodyVO> body = new ArrayList<InitDatasBodyVO>();
		return body;
	}

	@Override
	public ArrayList<InitDatasBodyVO> getBodyDefault() {
		ArrayList<InitDatasBodyVO> bodyDefault = new ArrayList<InitDatasBodyVO>();
		return bodyDefault;
	}
	
	/**
	 * 获取模板配置
	 */
	@Override
	public <T extends Serializable> InitDatasVO getLayout(T obj){
		CustomerVO vo = (CustomerVO) obj;
		//Long biztypeId = vo.getBiztypeId();
		long biztypeId = 11;
		return layoutService.getInitDatasForMobile(biztypeId);
	}
	
	@Override
	public <T extends Serializable> ArrayList<InitDatasListItemVO> getHeader(T obj, InitDatasVO initData) {
		ArrayList<InitDatasListItemVO> headerList = initData.getHeaderlist();
		
		CustomerVO vo = (CustomerVO) obj;
		Class<?> className = vo.getClass();
		
		if ( headerList != null && headerList.size() > 0) {
			for(InitDatasListItemVO headerItem : headerList) {
				String itemkey = headerItem.getItemkey();
				List<InitDatasItemVO> itemDataList = headerItem.getItemdatalist();
				InitDatasItemVO itemdata = itemDataList.get(0);
				Object dataid = "";
				Object datavalue = "";
				if ("province_city_district".equals(itemkey)) {
					//给省市区添加扩展结构(经纬度)
					List<InitParamItemVO> paramList = new ArrayList<>();
					InitParamItemVO paramItem;
					paramItem = new InitParamItemVO("longitude","");
					paramList.add(paramItem);
					paramItem = new InitParamItemVO("latitude","");
					paramList.add(paramItem);
					itemdata.setParamlist(paramList);
					//处理省市区的值
					dataid = vo.getProvince()+"_"+vo.getCity()+"_"+vo.getDistrict();
					datavalue = getAddressInfo(vo.getProvinceName(), vo.getCityName(), vo.getDistrictName(), "");
				} else if("ownerUserId".equals(itemkey)) { 
					//TODO目前只获取一个负责人信息
					//获取负责人信息
					List<CustomerSalesVO> saleList = vo.getSalesVOs();
					if (saleList != null) {
						CustomerSalesVO sale = saleList.get(0);
						if (sale != null) {
							dataid = (sale.getOwnerUserId() == null) ? "" : sale.getOwnerUserId();
							datavalue = (sale.getOwnerUserName() == null) ? "" : sale.getOwnerUserName();
						}
					}
				} else {
					try {
						//首字母大写
						char[] cs = itemkey.toCharArray();
						cs[0] = Character.toUpperCase(cs[0]);
						String funcName = String.valueOf(cs);
						//获取方法名称
						Method getObject = className.getMethod("get"+funcName);
						dataid = getObject.invoke(vo);
						Object data;
						if ("name".equals(itemkey)) {
							//给客户名称添加扩展结构(客户认证)
							List<InitParamItemVO> paramList = new ArrayList<>();
							InitParamItemVO paramItem;
							paramItem = new InitParamItemVO("isIdentified","N");
							paramList.add(paramItem);
							itemdata.setParamlist(paramList);
							datavalue = (dataid == null) ? "" : dataid.toString();
						} else if (Arrays.asList(itemkeyOfEnumList).contains(itemkey)) {
							//处理枚举类型
							Method getObjectName = className.getMethod("get"+funcName+"Name");
							data = getObjectName.invoke(vo);
							datavalue = (data == null) ? "" : data.toString();
						} else if (Arrays.asList(itemkeyOfPicList).contains(itemkey)) {
							datavalue = getPicUrl(dataid);
						} else {
							datavalue = (dataid == null) ? "" : dataid.toString();
						}
					} catch (Exception e) {
						throw new CrmBusinessException("动态调用客户方法错误！", e);
					}
				}
				itemdata.setDataid(dataid);
				itemdata.setDatavalue(datavalue);
			}
		} else {
			headerList = new ArrayList<InitDatasListItemVO>();
		}
		return headerList;
	}

	@Override
	public <T extends Serializable> ArrayList<InitDatasBodyVO> getBody(T obj, InitDatasVO initData) {
		ArrayList<InitDatasBodyVO> body = new ArrayList<InitDatasBodyVO>();
		//CustomerVO vo = (CustomerVO) obj;
		return body;
	}
	
}
